package com.yeu;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;



@WebServlet("/GetReplyTopic")
public class GetReplyTopic extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public GetReplyTopic() {
		super();
	}

	/** 获取真实用户IP **/
	public String getRemortIP(HttpServletRequest request) {
		if (request.getHeader("x-forwarded-for") == null) {
			return request.getRemoteAddr();
		} else {
			return request.getHeader("x-forwarded-for");
		}
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		System.out.println("Get");
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		response.setContentType("text/html;charset=utf-8"); // 设置响应报文的编码格式
		PrintWriter pw = response.getWriter(); // 获取 response 的输出流
		String replyTopic = "<hr>\n";
		String topicId = request.getParameter("topicId");
		String ipAddress = getRemortIP(request);
		System.out.println("topicId:" + topicId  + "\nipAddress:" + ipAddress); // 打印出来看一看
		System.out.println("【-- 获取说说 --】");
		
		String resCode = "";
		/* 获取信息模块 */
		try {
			Connection connect = DBUtil.getConnect();
			Statement statement = (Statement) connect.createStatement(); // Statement可以理解为数据库操作实例，对数据库的所有操作都通过它来实现
			ResultSet result;
			
			String sqlQuery = "select * from " + DBUtil.TABLE_USER_TOPIC_REPLY +" left join " + DBUtil.TABLE_USER_INFO + " on " + DBUtil.TABLE_USER_TOPIC_REPLY +".userNumber=" + DBUtil.TABLE_USER_INFO + ".userNumber where topicId = " + topicId + " order by replyDate desc";
			// 查询类操作返回一个ResultSet集合，没有查到结果时ResultSet的长度为0
			result = statement.executeQuery(sqlQuery); // 先查询同样的账号（比如手机号）是否正确
			
			while (result.next()) {
				replyTopic = replyTopic + "                            <div style=\"margin-left: 15px\">\n" +
		                "                                <img src=\"userHead/"+ result.getString("userHead") +".jpg\" class=\"img-circle\" width=\"30px\" style=\"float: left\"/>\n" +
		                "                                <div style=\"float: left;margin-left: 10px\">\n" +
		                "                                    <p class=\"h6\" style=\"margin-top: 0;color:#999\">"+ result.getString("userName") +"</p>\n" +
		                "                                    <p class=\"h6\" style=\"color:#999\">"+ result.getString("replyDate").substring(0,19) +"</p>\n" +
		                "                                </div>\n" +
		                "                                <div class=\" clearfix\"></div>\n" +
		                "                                <p class=\"h5\" style=\"margin-top: 5px;margin-left: 40px\">"+ result.getString("replyContent") +"</p>\n" +
		                "                            </div>\n";
				
			}
			if (replyTopic.equals("<hr>\n")) {
				pw.print("0");
				pw.flush();
				//断开数据库连接
				statement.close();
				return;
			}
			pw.println(replyTopic);
			pw.flush();
			//断开数据库连接
			statement.close();
			
		} catch (SQLException e) {
			e.printStackTrace();
		}

		
	}

}